/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rh.game.loader;

import java.io.InputStream;
import java.io.IOException;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 *
 * @author Hayden
 */
public class Loader {
    static IOException close(InputStream is)  {
        if (is == null)  return null;
        IOException ioe = null;

        try  {
            is.close();
        }
        catch (IOException x)  {
            ioe = x;
        }
        
        return ioe;
    }
    
    public static void main(String[] args)  {
        System.out.println("Begin: Loader");
        
        try  {
            Loader l = new Loader();
            String file = "/resources/games/game1.xml";
            WorldBuffer wb = l.load(file);
            
            System.out.println("Loader... npcs...");
            for (Object x : wb.getNpcs())  {
                System.out.println(x);
            }
        }
        catch (Throwable x)  {
            x.printStackTrace();
        }
        
        System.out.println("End");
    }
    
    public static WorldBuffer load(String pResource) throws Exception {
        WorldHandler wh = new WorldHandler();
            
        SAXParserFactory f = SAXParserFactory.newInstance();
        SAXParser saxp = f.newSAXParser();
        
        InputStream is = null;
        
        try  {
            is = Loader.class.getResourceAsStream(pResource);
            saxp.parse(is, wh);
        }
        finally  {
            close(is);
        }
        
        return wh.getWorldBuffer();
    }
}
